<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/6
 * Time: 16:16
 */

namespace app\web\controller;

use app\api\controller\v2\Auth;
use app\common\lib\astrolog\Astrolog;
use app\common\model\AppSignText;
use app\common\model\AstroPhaseText;
use app\common\model\AstroSignText;
use app\common\model\Recode;

class AstroExplain extends Auth
{
    /**
     * 获取星盘解读数据
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     */
    public function index()
    {
        if (!$this->request->isGet())
        {
            return show(config('code.error'), '未知操作');
        }

        $rid = input('rid', 0);

        if (!$rid)
        {
            return show(config('code.error'), '当前档案不存在');
        }

        $recode = (new Recode())->getRecode(['uid' => $this->user['id'], 'id' => $rid]);

        $natalData = Astrolog::getInstance()->setRecode1($recode)
            ->setExecPrefix(config('astrolog.prefix'))
            ->setIsAstrolabe(false)
            ->setUserSetting(config('astroset.setting1'))
            ->setGetDatas(['natal'])
            ->getAstroDatas()
            ->getDatas('natal');

        $phase_data = [];
        $array = [];

        // 获取行星配置信息
        $planet_info = config('astro');
        $planet_name = $planet_info['planet_name'];
        $planet_des = $planet_info['planet_des'];
        $planet_img = $planet_info['planet_img'];
        $planet_color = $planet_info['planet_color'];
        $planet_header_color = $planet_info['planet_header_color'];
        // 获取行星落宫落座图标和名称
        $sign_icon = $planet_info['sign_icon'];
        $house_icon = $planet_info['house_icon'];
        $sign_name = $planet_info['sign_name'];
        // 获取纤维图标和名称
        $phase_icon1 = $planet_info['phase_icon'];
        $phase_icon2 = $planet_info['phase_icon'];
        $phase_name1 = $planet_info['planet_name'];
        $phase_name2 = $planet_info['planet_name'];

        // 从数据库获取所有解读词条
        $sign_text = (new AppSignText())->getAllSignText();
        $house_text = (new AstroSignText())->getAllHouseText();
        $phase_text = (new AstroPhaseText())->getAllPhaseText();

        $data = [];
        // 获取解读词条
        foreach ($natalData['planets'] as $k => $v)
        {
            if ($v['id'] < 10)
            {
                // 获取行星信息
                $array['planet']['id'] = $v['id'];
                $array['planet']['name'] = $planet_name[$v['id']];
                $array['planet']['des'] = $planet_des[$v['id']];
                $array['planet']['img'] = $planet_img[$v['id']];
                $array['planet']['color'] = $planet_color[$v['id']];
                $array['planet']['header_color'] = $planet_header_color[$v['id']];
                // 获取行星落宫落座图标和名称
                $array['sign']['icon'] = $sign_icon[$v['in_sign_id']];
                $array['house']['icon'] = $house_icon[$v['in_house_id']];
                $array['sign']['name'] = $sign_name[$v['in_sign_id']];
                $array['house']['name'] = $v['in_house_id'];
                // 获取行星落宫落座词条
                $sign_data = [];
                foreach ($sign_text as $sign)
                {
                    if ($v['in_sign_id'] == $sign['sign_id'] && $v['id'] == $sign['planet_id'])
                    {
                        $sign_data[] = $sign['text'];
                    }
                }
                $array['sign']['text'] = $sign_data;
                $house_data = [];
                foreach ($house_text as $house)
                {
                    if ($v['in_house_id'] == $house['tid'] && $v['id'] == $house['planet'])
                    {

                        $last_str = mb_substr($house['text'], -1);
                        if ($last_str == '。')
                        {
                            $house['text'] = mb_substr($house['text'], 0, -1) . '；';
                        } elseif (mb_substr($house['text'], -1) != '；')
                        {
                            $house['text'] .= '；';
                        }
                        $house_data[] = $house['text'];
                    }
                }
                $array['house']['text'] = $house_data;

                $array['phase'] = [];
                if (is_array($v['phase']) && !empty($v['phase']))
                {
                    foreach ($v['phase'] as $kk => $vv)
                    {
                        $planet = explode('-', $kk);

                        if ($planet[0] < 10 && $planet[1] < 10)
                        {
                            // 获取行星相位图标和名称
                            $phase_data['icon1'] = $phase_icon1[$planet[0]];
                            $phase_data['icon2'] = $phase_icon2[$planet[1]];
                            $phase_data['name1'] = $phase_name1[$planet[0]];
                            $phase_data['name2'] = $phase_name2[$planet[1]];
                            $phase_data['degrees'] = $vv['phase_e'];

                            switch ($vv['phase_e'])
                            {
                                case 0:
                                    $degrees = 1;
                                    break;
                                case 60:
                                    $degrees = 2;
                                    break;
                                case 90:
                                    $degrees = 3;
                                    break;
                                case 120:
                                    $degrees = 4;
                                    break;
                                case 180:
                                    $degrees = 5;
                                    break;
                                default:
                                    $degrees = 0;
                                    break;
                            }
                            // 获取行星相位词条
                            $phase_array = [];
                            foreach ($phase_text as $phase)
                            {
                                if (($planet[0] == $phase['planet1'] && $planet[1] == $phase['planet2'] && $degrees == $phase['degrees']) ||
                                    ($planet[0] == $phase['planet2'] && $planet[1] == $phase['planet1'] && $degrees == $phase['degrees']))
                                {
                                    $last_str = mb_substr($phase['text'], -1);
                                    if ($last_str == '。')
                                    {
                                        $phase['text'] = mb_substr($phase['text'], 0, -1) . '；';
                                    } elseif (mb_substr($phase['text'], -1) != '；')
                                    {
                                        $phase['text'] .= '；';
                                    }
                                    $phase_array[] = $phase['text'];
                                }
                            }
                            $phase_data['text'] = $phase_array;
                            $array['phase'][] = $phase_data;
                        }
                    }
                }
            }
            $data[] = $array;
        }
        $data = super_unique($data, true);

        $phase_texts = [];

        foreach ($data as $k => $v)
        {
            foreach ($v['phase'] as $kk => $vv)
            {
                if ($vv['text'] && $vv['name1'] == $v['planet']['name'])
                {
                    $phase_texts[$k]['text'][] = $vv['text'];
                } else
                {
                    $phase_texts[$k]['text'][] = '';
                }
            }
        }
        return $this->fetch('index', ['data' => $data, 'phase_texts' => $phase_texts]);
    }
}
